<template>
	<a-row :gutter="10">
		<a-col :span="4">
			<a-card :bordered="false" :loading="cardLoading">
				<a-tree
					v-if="treeData.length > 0"
					v-model:expandedKeys="defaultExpandedKeys"
					:tree-data="treeData"
					:field-names="treeFieldNames"
					@select="treeSelect"
				/>
				<a-empty v-else :image="Empty.PRESENTED_IMAGE_SIMPLE" />
			</a-card>
		</a-col>

		<a-col :span="20">
			<a-card :bordered="false" style="margin-bottom: 10px">
				<a-form ref="searchFormRef" name="advanced_search" class="ant-advanced-search-form" :model="searchFormState">
					<a-row :gutter="24">
						<a-col :span="8">
							<a-form-item name="title" label="文章标题">
								<a-input v-model:value="searchFormState.title" placeholder="请输入文章标题" />
							</a-form-item>
						</a-col>
						<a-col :span="8">
							<a-button type="primary" @click="tableRef.refresh(true)">
								<template #icon><SearchOutlined /></template>
								查询
							</a-button>
							<a-button class="snowy-buttom-left" @click="reset">
								<template #icon><redo-outlined /></template>
								重置
							</a-button>
						</a-col>
					</a-row>
				</a-form>
			</a-card>
			<a-card :bordered="false">
				<s-table
					ref="tableRef"
					:columns="columns"
					:data="loadData"
					:expand-row-by-click="true"
					:alert="options.alert.show"
					bordered
					:row-key="(record) => record.id"
					:row-selection="options.rowSelection"
				>
					<template #operator class="table-operator">
						<a-space>
							<a-button type="primary" @click="formRef.onOpen()">
								<template #icon><plus-outlined /></template>
								新增
							</a-button>
							<xn-batch-delete :selectedRowKeys="selectedRowKeys" @batchDelete="removeBatch" />
						</a-space>
					</template>
					<template #bodyCell="{ column, record }">
            <template v-if="column.dataIndex === 'cover'">
              <img :src="record.cover.url" style="width: 60px; height: 40px; border-radius: 2px;" />
            </template>
            <template v-if="column.dataIndex === 'tags'">
              <a-tag color="blue" v-for="(item,index) in record.tags" :key="index" style="margin-right: 10px;">{{ item }}</a-tag>
            </template>
            <template v-if="column.dataIndex === 'price'">
              <a-tag v-if="record.price===0"  color="green" >免费</a-tag>
              <a-tag v-else color="orange" >{{ record.price }}</a-tag>
            </template>
            <template v-if="column.dataIndex === 'visible'">
							<a-switch :loading="loading" :checked="record.visible=='SHOW'" @change="editVisible(record)" />
						</template>
						<template v-if="column.dataIndex === 'action'">
							<a @click="formRef.onOpen(record.id)">编辑</a>
							<a-divider type="vertical" />
							<a-popconfirm title="确定删除吗？" @confirm="remove(record.id)">
								<a-button type="link" danger size="small">删除</a-button>
							</a-popconfirm>
						</template>
					</template>
				</s-table>
			</a-card>
		</a-col>
	</a-row>
	<Form ref="formRef" @successful="tableRef.refresh()" />
</template>

<script setup name="sysOrg">
	import { Empty } from 'ant-design-vue'
	import { isEmpty } from 'lodash-es'
	import articleApi from '@/api/biz/articleApi'
  import columnApi from "@/api/biz/columnApi"
	import Form from './form.vue'

	const columns = [
		{
			title: '标题',
			dataIndex: 'title'
		},
		{
			title: '封面',
			dataIndex: 'cover'
		},
		{
			title: '浏览量',
			dataIndex: 'viewCount',
		},
    {
			title: '价格',
			dataIndex: 'price',
		},
    {
			title: '资源链接',
			dataIndex: 'link',
		},
    {
			title: '是否可见',
			dataIndex: 'visible',
		},
    {
			title: '标签',
			dataIndex: 'tags',
		},
    {
			title: '排序',
			dataIndex: 'sortCode',
		},
		{
			title: '操作',
			dataIndex: 'action',
			align: 'center',
			width: '150px'
		}
	]
	const selectedRowKeys = ref([])
	// 列表选择配置
	const options = {
		alert: {
			show: false,
			clear: () => {
				selectedRowKeys.value = ref([])
			}
		},
		rowSelection: {
			onChange: (selectedRowKey, selectedRows) => {
				selectedRowKeys.value = selectedRowKey
			}
		}
	}
	// 定义tableDOM
	const tableRef = ref(null)
	const formRef = ref()
	const searchFormRef = ref()
	const searchFormState = ref({})
	// 默认展开的节点
	const defaultExpandedKeys = ref([])
	const treeData = ref([])
	// 替换treeNode 中 title,key,children
	const treeFieldNames = { children: 'children', title: 'name', key: 'id' }
	const cardLoading = ref(true)
  const loading = ref(false)

	// 表格查询 返回 Promise 对象
	const loadData = (parameter) => {
		loadTreeData()
		return articleApi.page(Object.assign(parameter, searchFormState.value)).then((res) => {
			return res
		})
	}
	// 重置
	const reset = () => {
		searchFormRef.value.resetFields()
		tableRef.value.refresh(true)
	}
	// 加载左侧的树
	const loadTreeData = () => {
		columnApi.list().then((res) => {
			cardLoading.value = false
			if (res !== null) {
				treeData.value = res
			}
		})
	}
	// 点击树查询
	const treeSelect = (selectedKeys) => {
		if (selectedKeys.length > 0) {
			searchFormState.value.columnId = selectedKeys.toString()
		} else {
			delete searchFormState.value.columnId
		}
		tableRef.value.refresh(true)
	}
	// 删除
	const remove = (record) => {
		articleApi.remove([{id}]).then(() => {
			tableRef.value.refresh(true)
		})
	}
	// 批量删除
	const removeBatch = (params) => {
		articleApi.remove(params).then(() => {
			tableRef.value.clearRefreshSelected()
		})
	}
  // 修改状态
	const editVisible = (record) => {
		loading.value = true
		let params = {
      id: record.id,
      visible: record.visible=='SHOW'?'HIDE':'SHOW'
    }
    articleApi
      .editVisible(params)
      .then(() => {
        tableRef.value.refresh()
      })
      .finally(() => {
        loading.value = false
      })
	}
</script>

<style scoped>
	.ant-form-item {
		margin-bottom: 0 !important;
	}
	.primaryAdd {
		margin-right: 10px;
	}
	.snowy-buttom-left {
		margin-left: 8px;
	}
</style>
